package com.jml.zlhq.app.ui;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.ui.LoginActivity.CheckBind;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.CommonDialog;
import com.loopj.android.http.TextHttpResponseHandler;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MoneyActivity extends BaseActivity{
	private EditText userid,money,tix,xm;
	private TextView msg;
	private Button submit;
	//private String zfb,jine; 
	private float my;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.money_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		//LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		//rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setImageResource(R.drawable.back_selector);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("提现");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setText("  ");
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		
		msg = (TextView) findViewById(R.id.money_msg);
		userid = (EditText) findViewById(R.id.money_zfb);
		money = (EditText) findViewById(R.id.money_jine);
		xm = (EditText) findViewById(R.id.money_zfb_xm);
		money.setText("账户金币："+getIntent().getExtras().getString("jine"));
		userid.setText("支付宝："+getIntent().getExtras().getString("zfb"));
		xm.setText("姓名："+getIntent().getExtras().getString("zfbxm"));
		my = getIntent().getExtras().getFloat("jinef");
		tix = (EditText) findViewById(R.id.money_txjine);
//		tix.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                int number = 0;
//                try {
//                    number = Integer.parseInt(s.toString());
//                } catch (Exception e) {}
//                if (number>100&&number%100!=0) {
//                    number = number-(number%100);
//                    s = String.valueOf(number);
//                    tix.setText(s);
//                    tix.setSelection(s.length());
//                    return;
//                }
//            }
// 
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
// 
//            @Override
//            public void afterTextChanged(Editable s) {}
// 
//        });
// 
		submit = (Button) findViewById(R.id.money_submit);
		submit.setOnClickListener(this);
	}

	@Override
	public void loadData(Map<String, String> param) {}

	@Override
	public void clearData() {}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==201&&appContext.isLogin()){
			finish();	
		}
		if(requestCode==202&&appContext.isLogin()){
			finish();	
		}
		if(requestCode==203&&appContext.isLogin()){
			UIUtil.openActivity(activity, BindPhoneActivity.class, null,204);
		}
		if(requestCode==204&&appContext.isLogin()){
			finish();
		}
	}

	@Override
	public void onClick(View view) {
		msg.setText("");
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		case R.id.money_submit:
			String txm = tix.getText().toString();
			int txmf = 0;
			try {
				txmf = Integer.parseInt(txm);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(txmf<TanDianFragment.txxz||txmf>my){
				msg.setText("最低提现金币"+TanDianFragment.txxzstr+"且不能超过账户金币！");
			}else if (txmf%100!=0){
			    msg.setText("提现金币数必须为100整数倍！");
			}else{
				Map<String, String> param = new HashMap<String, String>();
				param.put("auth_token", appContext.getLoginUid());
				param.put("applyfees", tix.getText().toString());
				final Handler mHandler = new Handler(){
					@Override
					public void handleMessage(Message msg) {
						final CommonDialog show = new CommonDialog(activity, true);
						switch (msg.what) {
						case 0:
							LoginActivity.afterLogined(activity,new CheckBind() {
								@Override
								public void checkBind(String phone) {
									//TODO
								}
							});
							show.show(true, true, false, "提现", "成功，将尽快为您转账到支付宝！", "确定", "", new OnClickListener(){
								@Override
								public void onClick(View v) {
									show.dismiss();
									activity.finish();
								}
							}, null);
							break;
						default:
							show.show(true, true, false, "提现", "失败，系统出错，请联系管理员！", "确定", "", new OnClickListener(){
								@Override
								public void onClick(View v) {
									show.dismiss();
								}
							}, null);
							break;
						}
					}
				};
				HttpService.postByText(URLs.MAIN_USER_TIXIAN, param, new TextHttpResponseHandler() {
					@Override
					public void onSuccess(int arg0, Header[] arg1, String resultStr) {
						try {
							JSONObject result = new JSONObject(resultStr);
							mHandler.sendEmptyMessage(result.getInt("status"));
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
					
					@Override
					public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
						UIUtil.ToastMessage(activity, "抱歉，访问失败");
					}
				});
			}
			break;
		default:
			break;
		}
	}
}
